package com.example.baselib.utils.log;


import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

class LogImpl implements ILog {

    IConfig mIConfig;
    HiLogLabel label;

    public LogImpl(IConfig iConfig) {
        this.mIConfig = iConfig;
        label = new HiLogLabel(HiLog.LOG_APP, mIConfig.domain, mIConfig.tag);
    }


    @Override
    public void d(String format, Object... args) {
        if (!isLogger()) {
            return;
        }
        format = stringFormat(format);
        String lineNum = "";
        if (mIConfig.showLineNum) {
            lineNum = getLineNum();
            format = lineNum + format;
        }
        HiLog.debug(label, format, args);
    }

    @Override
    public void i(String format, Object... args) {
        if (!isLogger()) {
            return;
        }
        format = stringFormat(format);
        String lineNum = "";
        if (mIConfig.showLineNum) {
            lineNum = getLineNum();
            format = lineNum + format;
        }
        HiLog.info(label, format, args);
    }

    @Override
    public void w(String format, Object... args) {
        if (!isLogger()) {
            return;
        }
        format = stringFormat(format);
        String lineNum = "";
        if (mIConfig.showLineNum) {
            lineNum = getLineNum();
            format = lineNum + format;
        }
        HiLog.warn(label, format, args);
    }

    @Override
    public void e(String format, Object... args) {
        if (!isLogger()) {
            return;
        }
        format = stringFormat(format);
        String lineNum = "";
        if (mIConfig.showLineNum) {
            lineNum = getLineNum();
            format = lineNum + format;
        }
        HiLog.error(label, format, args);
    }

    @Override
    public boolean isLogger() {
        int hiLogLevel = getHiLogLevel(mIConfig.level);
        return HiLog.isLoggable(mIConfig.domain, mIConfig.tag, hiLogLevel);
    }

    private String stringFormat(String format) {
        if (format == null || format.length() == 0) {
            return format;
        }
        if (format.contains("%{public}") || format.contains("%{private}")) {
            return format;
        }
        if (!mIConfig.isPrivate) {
            if (format.contains("%s")) {
                //替换 字符串类型
                format = format.replace("%s", "%{public}s");
            }
            if (format.contains("%d")) {
                // 替换 整数类型
                format = format.replace("%d", "%{public}d");
            }
            if (format.contains("%f")) {
                // 替换 浮点类型
                format = format.replace("%f", "%{public}f");
            }
            if (format.contains("%b")) {
                // 替换 布尔类型
                format = format.replace("%b", "%{public}b");
            }
        }
        return format;
    }

    //获取类名与行号
    private String getLineNum() {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        try {
            StackTraceElement traceElements = stackTrace[5];
            String methodName = traceElements.getMethodName();
            return methodName +
                    "(" +
                    traceElements.getFileName() +
                    ":" +
                    traceElements.getLineNumber() +
                    ") ";
        } catch (Exception e) {
        }
        return "";
    }


    private int getHiLogLevel(int level) {
        switch (level) {
            case 1:
                return 3;
            case 2:
                return 4;
            case 3:
                return 5;
            case 4:
                return 6;
        }
        return 7;
    }


}
